home *** CD-ROM | disk | FTP | other *** search
- /**
- GRAB Graph Layout and Browser System
-
- Copyright (c) 1986, 1988 Regents of the University of California
- Copyright (c) 1989, Tera Computer Company
- **/
-
- /**
- Algorithm for detecting if a line passes through a rectangle. Adapted
- from the intersection algorithm in Bowyer & Woodwark's "A Programmer's
- Geometry" (p. 133), in turn due to Cohen and Sutherland.
- **/
-
- #include "attribute.h"
- #include "digraph.h"
- #include "screen.h"
-
- BOOL Intersect();
-
- Abs(x)
- {
- if (x < 0)
- {
- return -x;
- }
- else
- {
- return x;
- }
- }
-
- Quadrant(x, y, box)
- int x, y;
- BOX *box;
- {
- return (
- 1 * (x < box->min_x) +
- 2 * (x > box->max_x) +
- 4 * (y < box->min_y) +
- 8 * (y > box->max_y)
- );
- }
-
-
- BOOL Intersect(x1, y1, x2, y2, bx1, by1, bx2, by2)
- int x1, y1, x2, y2, bx1, by1, bx2, by2;
- {
- BOX *b;
- int midX, midY, midQuad;
-
- b = (BOX *) malloc (sizeof(BOX));
- b->min_x = MIN(bx1, bx2);
- b->max_x = MAX(bx1, bx2);
- b->min_y = MIN(by1, by2);
- b->max_y = MAX(by1, by2);
-
- if ((Quadrant(x1, y1, b) & Quadrant(x2, y2, b)) != 0)
- {
- return FALSE;
- }
-
- do
- {
- midX = (x1 + x2)/2;
- midY = (y1 + y2)/2;
- midQuad = Quadrant(midX, midY, b);
-
- if (midQuad == 0)
- {
- return TRUE;
- }
- else if ((Quadrant(x1, y1, b) & midQuad) != 0)
- {
- x1 = midX;
- y1 = midY;
- }
- else
- {
- x2 = midX;
- y2 = midY;
- }
- } while (MAX(Abs(x1-x2), Abs(y1-y2)) > 1);
-
- return FALSE;
- }
-